define([ "app", "jquery", "md5", "ecb","artDialog","iframeTools", "services/UserService",'directives/ImageCrop',
		"resources/UserResource","resources/ContactsResource", "resources/ShowTimesUserResource" ],
		function(app) {

			var deps = [ "$scope", "$state", "$window", "$location", "$stateParams", "UserService", "UserResource","ContactsResource", "ShowTimesUserResource" ];

			function controller($scope, $state, $window, $location, $stateParams, UserService, UserResource,ContactsResource, ShowTimesUserResource) {
				
				initDate($scope)
				
				var imageData;
				$scope.saveUser = function() {

					if (imageData && imageData.file) {
						uploadImage().success(
								function(result) {
									$scope.userVo.userPicUrl = eval("("
											+ result.fileId + ")").obj;
									saveOrUpdate();
								});
					} else {
						saveOrUpdate();
					}
				};
				

				/**
				  * 图片数据
				 */
				$scope.$bus.subscribe({
					channel : 'image',
					topic : 'crop',
					callback : function(data) {
						imageData = data;
					}
				});
	            
				
				/**
				 * 选择岗位
				 */
				$scope.checkposition =function (){
					
					var positionId = $scope.positionId;
					if (positionId == null || positionId == "" || typeof (positionId) == 'undefined') {
						$scope.userCreateForm.positionId.$error.required=true;
						$scope.userCreateForm.positionId.$dirty=true;
					}else{
						$scope.userCreateForm.positionId.$error.required=false;
						$scope.userCreateForm.positionId.$dirty=false;
					}
				}

				/**
				 * 选择岗位类型
				 */
	            $scope.checkGroup = function() {

					var groupId = $scope.groupId;
					if (groupId == null || groupId == "" || typeof (groupId) == 'undefined') {
						$scope.positionList = [

						];
						$scope.userCreateForm.groupId.$error.required=true;
						$scope.userCreateForm.groupId.$dirty=true;
					}else{
						$scope.userCreateForm.groupId.$error.required=false;
						$scope.userCreateForm.groupId.$dirty=false;
					}
					
					
					
					if (groupId == "1") {
						$scope.positionList = [ {
							id : "1",
							name : "产品工程师"
						}, {
							id : "2",
							name : "产品经理"
						} ];
						$scope.positionId=$scope.positionList[0].id;
					}

					if (groupId == "2") {
						$scope.positionList = [ {
							id : "1",
							name : "测试工程师"
						} ];
						$scope.positionId=$scope.positionList[0].id;
					}

					if (groupId == "3") {
						$scope.positionList = [ {
							id : "1",
							name : "实习生"
						}, {
							id : "2",
							name : "java初级开发"
						}, {
							id : "3",
							name : "java中级开发"
						}, {
							id : "4",
							name : "java高级开发"
						}, {
							id : "5",
							name : "java架构师"
						} ];
						$scope.positionId=$scope.positionList[0].id;
					}

					if (groupId == "4") {
						$scope.positionList = [ {
							id : "1",
							name : "部门经理"
						}, {
							id : "2",
							name : "湖北研发中心主任"
						} ];
						$scope.positionId=$scope.positionList[0].id;
					}
				}
				
				$scope.close = function() {
					$state.go('home.userManagement');
				}
				
				
				/** 按钮状态 **/
				$scope.$watch('userCreateForm.$invalid', function(value) {

					// 无效
					if(value){
						$("#confirm").addClass("confirm_btn_x");
						$("#confirm").removeClass("confirm_btn1 ");
					}
					else{
						$("#confirm").addClass("confirm_btn1");
						$("#confirm").removeClass("confirm_btn_x ");
					}
		        });
				
				
				function saveOrUpdate(){
					
					if($scope.userVo ==null){
						art.dialog.alert("请输入相应信息");
						return false;
					}
					if($scope.userVo.password !=null && $scope.userVo.password =="" && typeof $scope.userVo.password =='undefined'){
						var password =encrypt($scope.userVo.password, $scope.userVo.userName);
						$scope.userVo.password=password;
					}
					
					if($scope.groupId==null || $scope.groupId==""){
						//art.dialog.alert("请选择岗位类型");
						$scope.userCreateForm.groupId.$error.required=true;
						$scope.userCreateForm.groupId.$dirty=true;
						return false;
					}
					
					if($scope.positionId==null || $scope.positionId=="" || typeof $scope.positionId =='undefined'){
						$scope.userCreateForm.positionId.$error.required=true;
						$scope.userCreateForm.positionId.$dirty=true;
						return false;
					}
					if ($scope.userCreateForm.$invalid) {
						$("#confirm").addClass("confirm_btn_x");
						$("#confirm").removeClass("confirm_btn1 ");
						return false;
					}
					$scope.userCreateForm.$invalid = true;
					
					// 修改下拉框的值
					$scope.userVo.sex=$scope.sex;
					$scope.userVo.positionId=$scope.positionId;
					$scope.userVo.groupId=$scope.groupId;
					$scope.userVo.userType=$scope.userType;
					
					ShowTimesUserResource.save($scope.userVo).success(function(data){
						$scope.userCreateForm.$invalid = false;
						if (data.resCode=='0000') {
							//$modalInstance.close($scope.userVo);
						}else{
							art.dialog.alert("新增用户失败");
							//$modalInstance.close($scope.userVo);
						}
						$state.go('home.userManagement');
		            });
					
				}
				
				/**
	             * 图片剪裁
	             */
	            function uploadImage() {
	                var formData = new FormData();
	                formData.append("file", imageData.file);

	                var cropParam = {
	                    cropX: imageData.cropX || 0,
	                    cropY: imageData.cropY || 0,
	                    cropW: imageData.cropW || 0,
	                    cropH: imageData.cropH || 0
	                };
	                return ContactsResource.uploadCropImage(formData, cropParam);
	            }
	            
				/**
				 * 初始化下拉列表
				 */
				function initDate(AddYokiUserResource){
					$scope.userTypeList = [
					    {id : "0", name : "普通用户"},
					    {id : "2", name : "后台用户"}
					];
					
					$scope.groupList = [
					    {id : "1", name : "产品岗"},
					    {id : "2", name : "测试岗"},
					    {id : "3", name : "开发岗"},
					    {id : "4", name : "管理岗"}
					];
					
					$scope.positionList = [ {
						id : "1",
						name : "产品工程师"
					}, {
						id : "2",
						name : "产品经理"
					} ];
					
					$scope.sexList = [
					    {id : "1", name : "男"},
					    {id : "2", name : "女"}
					];
					
					$scope.groupId=$scope.groupList[0].id;
					$scope.sex=$scope.sexList[0].id;
					$scope.userType=$scope.userTypeList[0].id;
					$scope.positionId=$scope.positionList[0].id;
				}
			}
			
			/**
			 * 密码 encrypt 加密
			 */
			function encrypt(content, srcKey) {
				var keyLen = 8;
				var srcKeyLen = srcKey.length;

				if (srcKeyLen > keyLen) {
					srcKey = srcKey.substr(0, keyLen);

				} else {
					var paddLen = keyLen - srcKeyLen;
					for (var i = 0; i < paddLen; i++) {
						srcKey += "0";
					}
				}

				var key = CryptoJS.enc.Utf8.parse(srcKey);
				var encryptStr = CryptoJS.DES.encrypt(content, key, {
					mode : CryptoJS.mode.ECB,
					padding : CryptoJS.pad.Pkcs7
				});
				return encryptStr.toString();
			}
			
			controller.$inject = deps;
			return app.lazy.controller("AddUserController", controller);
		});
